package com.zhiyou.crud.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhiyou.crud.bean.Manage;
import com.zhiyou.crud.bean.News;
import com.zhiyou.crud.bean.Question;
import com.zhiyou.crud.service.Admin_ManageService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Description: Admin_ManageController
 * @Author: ZengS
 * @Date: 2022/9/24 17:14
 */
@Controller
public class Admin_ManageController {
    @Resource
    Admin_ManageService admin_manageService;

    /**
     * 查询所有新闻(咨询)
     */
    @RequestMapping("/admin/getNews")
    public String getNews(Model model,@RequestParam(value = "page",defaultValue = "1") Integer page){
        //        session.setAttribute("con",con);
        PageHelper.startPage(page,5);
        List<News> newsList = admin_manageService.getNews();

        System.out.println("newsList = " + newsList);

        PageInfo pageInfo = new PageInfo(newsList);
        model.addAttribute("pageinfo",pageInfo);
        return "/admin/admin_news";
    }
    /**
     * 发布新闻
     */
    @RequestMapping("/admin/insertNews")
    public String insertNews(News news){
        admin_manageService.insertNews(news);
        return "redirect:/admin/getNews";
    }
    /**
     * 删除新闻
     */
    @RequestMapping("/admin/deleteNews")
    public String deleteNews(Integer nno){
        admin_manageService.deleteNews(nno);
        return "redirect:/admin/getNews";
    }
    /**
     * 更新新闻
     */
    @RequestMapping("/admin/updateNews")
    public String updateNews(News news){
        admin_manageService.updateNews(news);
        return "redirect:/admin/getNews";
    }
    /**
     * 查询所有问题
     */
    @RequestMapping("/admin/getQuestion")
    public String getQuestion(Model model,@RequestParam(value = "page",defaultValue = "1") Integer page){
        //        session.setAttribute("con",con);
        PageHelper.startPage(page,5);
        List<Question> newsList = admin_manageService.getQuestion();

        System.out.println("newsList = " + newsList);

        PageInfo pageInfo = new PageInfo(newsList);
        model.addAttribute("pageinfo",pageInfo);
        return "/admin/admin_question";
    }
    /**
     * 新增常见问题
     */
    @RequestMapping("/admin/insertQuestion")
    public String insertQuestion(Question question){
        admin_manageService.insertQuestion(question);
        return "redirect:/admin/getQuestion";
    }
    /**
     * 删除常见问题
     */
    @RequestMapping("/admin/deleteQuestion")
    public String deleteQuestion(Integer qno){
        admin_manageService.deleteQuestion(qno);
        return "redirect:/admin/getQuestion";
    }
    /**
     * 更新常见问题
     */
    @RequestMapping("/admin/updateQuestion")
    public String updateQuestion(Question question){
        admin_manageService.updateQuestion(question);
        return "redirect:/admin/getQuestion";
    }
    /**
     * 更新管理员账户
     */
    @RequestMapping("/admin/updateManage")
    public String updateManage(Manage manage, HttpSession session){
        session.removeAttribute("manage");
        admin_manageService.updateManage(manage);
        return "/admin/admin_login";
    }
}
